package game.module.itemnew.logic;

import game.common.ErrorCode;
import game.constants.Position;
import game.entity.Hero;
import game.module.itemnew.beans.ItemStorage.Item;
import game.module.itemnew.beans.ItemTemplate;
import game.module.itemnew.dao.ItemCache;
import game.module.itemnew.dao.ItemTemplateCache;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemAction {

	@Autowired
	private ItemCache itemCache;

	@Autowired
	private ItemTemplateCache itemTemplateCache;

	@Autowired
	private ItemManager itemManager;

	/**
	 * 删除道具
	 * 
	 * @param heroId
	 * @param pos
	 * @return
	 */
	public int removeBagItem(int heroId, int pos) {
		Map<Short, Item> bagItemMap = itemCache.getBagGoodsMap(heroId);
		Item item = bagItemMap.get(pos);
		if (item == null) {
			return 1;
		}
		bagItemMap.remove(pos);
		return 0;
	}

	/**
	 * 根据模板添加道具
	 * 
	 * @param hero
	 * @param templateId
	 * @param count
	 * @param isBind
	 * @return
	 */
	public int addItemByTemplate(Hero hero, int templateId, int count, boolean isBind) {
		ItemTemplate it = itemTemplateCache.getItemTplById(templateId);
		if (it == null) {
			return 2;
		}
		short freeCount = itemManager.getBagFreeCellCount(hero.getId(), hero.getBagCapacity());
		int maxCapasity = it.getRepeat() * freeCount;
		if (count > maxCapasity) {
			return 3;
		}
		int putCount = count;
		Set<Short> posList = itemCache.getBagGoodsMap(hero.getId()).keySet();
		for (short i = Position.BagGoodsBeginPostion; i <= Position.BagGoodsBeginPostion + hero.getBagCapacity() && putCount > 0; i++) {
			if (!posList.contains(i)) {
				int curPutCount = 0;
				if (putCount > it.getRepeat()) {
					curPutCount = it.getRepeat();
				} else {
					curPutCount = putCount;
				}
				Item item = new Item();
				item.setId(0);
				item.setBind(isBind);
				item.setCount(curPutCount);
				item.setCurrDurability(it.getDurability());
				item.setMaxDurability(it.getDurability());
				item.setGoodmodelId(it.getId());
				item.setPosition(Integer.valueOf(i));
				itemCache.getBagGoodsMap(hero.getId()).put(i, item);
				putCount -= curPutCount;
			}
		}
		return ErrorCode.SUCCESS;
	}

	/**
	 * 移动背包物品
	 * 
	 * @param hero
	 * @param oldPos
	 * @param newPos
	 * @return
	 */
	public int moveItemToCell(Hero hero, short oldPos, short newPos) {
		int heroId = hero.getId();
		// 原位置物品是否存在
		Item item = itemCache.getBagGoodsMap(heroId).get(oldPos);
		if (item == null) {
			return 4;
		}
		// 目标位置是否有物品
		Item targetItem = itemCache.getBagGoodsMap(heroId).get(newPos);
		if (targetItem == null) {
			// 替换
			item.setPosition(Integer.valueOf(newPos));
			itemCache.getBagGoodsMap(heroId).remove(oldPos);
			itemCache.getBagGoodsMap(heroId).put(newPos, item);
		} else {
			// TODO 交换位置
			// TODO 堆叠
			// TODO 绑定非绑定判断
		}
		return ErrorCode.SUCCESS;
	}

	/**
	 * 整理背包
	 * 
	 * @return
	 */
	public int sortBag() {
		return ErrorCode.SUCCESS;
	}

	/**
	 * 拆分物品
	 * 
	 * @param heroId
	 * @return
	 */
	public int splitBagItem(Hero hero, short itemPos, short count) {
		int heroId = hero.getId();
		if (count <= 0) {
			return ErrorCode.COUNT_LESS_THAN_0;
		}
		Map<Short, Item> bagItemMap = itemCache.getBagGoodsMap(heroId);
		Item item = bagItemMap.get(itemPos);
		if (item == null) {
			return ErrorCode.ITEM_NOT_EXIST;
		}
		int oldCount = item.getCount();
		if (oldCount <= count) {
			return ErrorCode.ARGUMENT_INVALID;
		}
		short firstFreeCell = itemManager.getFirstFreeCell(heroId, hero.getBagCapacity());
		if (firstFreeCell == 0) {
			return ErrorCode.ITEM_NO_FREE_CELL;
		}
		item.setCount(oldCount - count);
		Item itemNew = new Item();
		itemNew.setBind(item.getBind());
		itemNew.setCount(Integer.valueOf(count));
		itemNew.setCurrDurability(item.getCurrDurability());
		itemNew.setGoodmodelId(item.getGoodmodelId());
		itemNew.setPosition(Integer.valueOf(firstFreeCell));
		bagItemMap.put(firstFreeCell, itemNew);
		return ErrorCode.SUCCESS;
	}

}
